package com.hejia.bulbul.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.DayCount;
import com.hejia.alauda.entity.UserOrderDayStats;
import com.hejia.alauda.entity.example.UserOrderDayStatsExample;
import com.hejia.alauda.persistence.mybatis.UserOrderDayStatsMapper;
import com.hejia.alauda.utils.DateTimeUtil;
import com.hejia.alauda.utils.Pager;
import com.hejia.bulbul.entity.PopularizeStatistic;

@Service
public class UserOrderDayStatsServiceImpl implements UserOrderDayStatsService {
	
	@Resource
	private UserOrderDayStatsMapper userOrderDayStatsMapper;
	
	@Resource
	private PopularizeStatisticService popularizeStatisticService;
	
	@Override
	public List<DayCount> statsDayRegisteredUser(Date startDate, Date endDate) {
		return this.userOrderDayStatsMapper.statsDayRegisteredUser(startDate, endDate);
	}

	@Override
	public void addOrUpdateDayRegisteredUserStats(DayCount dayCount) {
		List<UserOrderDayStats> userOrderDayStatses = findUserOrderDayStatsByDayCount(dayCount);
		UserOrderDayStats userOrderDayStats = null;
		if (userOrderDayStatses.size() > 0) {
			userOrderDayStats = userOrderDayStatses.get(0);
			userOrderDayStats.setModifyDate(new Date());
			userOrderDayStats.setRegisteredUserCount(dayCount.getCount());
			this.userOrderDayStatsMapper.updateByPrimaryKey(userOrderDayStats);
		} else {
			userOrderDayStats = createUserOrderDayStats(dayCount);
			userOrderDayStats.setRegisteredUserCount(dayCount.getCount());
			this.userOrderDayStatsMapper.insert(userOrderDayStats);
		}
	}

	/**
	 * 创建用户订单按天统计
	 * @param dayCount
	 * @return
	 */
	private UserOrderDayStats createUserOrderDayStats(DayCount dayCount) {
		UserOrderDayStats userOrderDayStats = new UserOrderDayStats();
		userOrderDayStats.setCreateDate(new Date());
		userOrderDayStats.setModifyDate(new Date());
		userOrderDayStats.setSourceId(dayCount.getSourceId());
		userOrderDayStats.setStatsTime(dayCount.getDate());
		return userOrderDayStats;
	}
	
	/**
	 * 查询用户按天统计记录
	 * @param dayCount
	 * @return
	 */
	private List<UserOrderDayStats> findUserOrderDayStatsByDayCount(DayCount dayCount) {
		UserOrderDayStatsExample example = new UserOrderDayStatsExample();
		example.appendCriterion("sourceId =", dayCount.getSourceId());
		example.appendCriterion("statsTime =", dayCount.getDate());
		return this.userOrderDayStatsMapper.selectByExample(example, null);
	}

	@Override
	public List<DayCount> statsDayNewTradeUserAndAmount(Date startDate, Date endDate) {
		return this.userOrderDayStatsMapper.statsDayNewTradeUserAndAmount(startDate, endDate);
	}

	@Override
	public void addOrUpdateDayNewTradeUserAndAmountStats(DayCount dayCount) {
		List<UserOrderDayStats> userOrderDayStatses = findUserOrderDayStatsByDayCount(dayCount);
		UserOrderDayStats userOrderDayStats = null;
		if (userOrderDayStatses.size() > 0) {
			userOrderDayStats = userOrderDayStatses.get(0);
			userOrderDayStats.setModifyDate(new Date());
			userOrderDayStats.setNewTradeUserCount(dayCount.getCount());
			userOrderDayStats.setNewTradeUserOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.updateByPrimaryKey(userOrderDayStats);
		} else {
			userOrderDayStats = createUserOrderDayStats(dayCount);
			userOrderDayStats.setNewTradeUserCount(dayCount.getCount());
			userOrderDayStats.setNewTradeUserOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.insert(userOrderDayStats);
		}
	}

	@Override
	public List<DayCount> statsDayOrder(Date startDate, Date endDate) {
		return this.userOrderDayStatsMapper.statsDayOrder(startDate, endDate);
	}

	@Override
	public void addOrUpdateDayOrderStats(DayCount dayCount) {
		List<UserOrderDayStats> userOrderDayStatses = findUserOrderDayStatsByDayCount(dayCount);
		UserOrderDayStats userOrderDayStats = null;
		if (userOrderDayStatses.size() > 0) {
			userOrderDayStats = userOrderDayStatses.get(0);
			userOrderDayStats.setModifyDate(new Date());
			userOrderDayStats.setOrderCount(dayCount.getCount());
			this.userOrderDayStatsMapper.updateByPrimaryKey(userOrderDayStats);
		} else {
			userOrderDayStats = createUserOrderDayStats(dayCount);
			userOrderDayStats.setOrderCount(dayCount.getCount());
			this.userOrderDayStatsMapper.insert(userOrderDayStats);
		}
	}

	@Override
	public List<DayCount> statsDaySuccessOrderAndAmount(Date startDate, Date endDate) {
		return this.userOrderDayStatsMapper.statsDaySuccessOrderAndAmount(startDate, endDate);
	}

	@Override
	public void addOrUpdateDaySuccessOrderAndAmountStats(DayCount dayCount) {
		List<UserOrderDayStats> userOrderDayStatses = findUserOrderDayStatsByDayCount(dayCount);
		UserOrderDayStats userOrderDayStats = null;
		if (userOrderDayStatses.size() > 0) {
			userOrderDayStats = userOrderDayStatses.get(0);
			userOrderDayStats.setModifyDate(new Date());
			userOrderDayStats.setSuccessOrderCount(dayCount.getCount());
			userOrderDayStats.setSuccessOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.updateByPrimaryKey(userOrderDayStats);
		} else {
			userOrderDayStats = createUserOrderDayStats(dayCount);
			userOrderDayStats.setSuccessOrderCount(dayCount.getCount());
			userOrderDayStats.setSuccessOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.insert(userOrderDayStats);
		}
	}

	@Override
	public List<DayCount> statsDayRepeatOrderAndAmount(Date startDate, Date endDate) {
		return this.userOrderDayStatsMapper.statsDayRepeatOrderAndAmount(startDate, endDate);
	}

	@Override
	public void addOrUpdateDayRepeatOrderAndAmountStats(DayCount dayCount) {
		List<UserOrderDayStats> userOrderDayStatses = findUserOrderDayStatsByDayCount(dayCount);
		UserOrderDayStats userOrderDayStats = null;
		if (userOrderDayStatses.size() > 0) {
			userOrderDayStats = userOrderDayStatses.get(0);
			userOrderDayStats.setModifyDate(new Date());
			userOrderDayStats.setRepeatOrderCount(dayCount.getCount());
			userOrderDayStats.setRepeatOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.updateByPrimaryKey(userOrderDayStats);
		} else {
			userOrderDayStats = createUserOrderDayStats(dayCount);
			userOrderDayStats.setRepeatOrderCount(dayCount.getCount());
			userOrderDayStats.setRepeatOrderAmount(dayCount.getAmount());
			this.userOrderDayStatsMapper.insert(userOrderDayStats);
		}
	}

	@Override
	public Pager<PopularizeStatistic> statisticPopularizes(Date startDate, Date endDate, Pager<PopularizeStatistic> pager) {
		
		pager = this.popularizeStatisticService.findPopularizeStatisticByBiggerCategory(pager);
		List<PopularizeStatistic> statistics = pager.getList();
		
		if(statistics.size() > 0){
			this.popularizeStatisticService.setVisitIPCount(statistics, startDate, endDate);
			List<Long> biggerCategoryIds = this.getBiggerCategoryIds(statistics);
			List<UserOrderDayStats> userOrderDayStatses = this.userOrderDayStatsMapper
					.sumUserOrderByBiggerCatogoryIds(biggerCategoryIds, startDate, endDate);
			for (UserOrderDayStats stats : userOrderDayStatses) {
				for (PopularizeStatistic statistic : statistics) {
					if (statistic.getBiggerCategoryId().equals(stats.getBiggerCategoryId())) {
						statistic.setRegisterCount(stats.getRegisteredUserCount());
						statistic.setNewTradeUserCount(stats.getNewTradeUserCount());
						statistic.setOrderCount(stats.getOrderCount());
						statistic.setPaidOrderCount(stats.getSuccessOrderCount());
						statistic.setPaymentAmount(stats.getSuccessOrderAmount());
					}
				}
			}
		}
		
		return pager;
	}
	
	/**
	 * 获取稍大类别ID列表
	 * @param statistics
	 * @return
	 */
	private List<Long> getBiggerCategoryIds(List<PopularizeStatistic> statistics) {
		List<Long> biggerCategoryIds = new ArrayList<>();
		for (PopularizeStatistic statistic : statistics) {
			biggerCategoryIds.add(statistic.getBiggerCategoryId());
			
		}
		return biggerCategoryIds;
	}

	@Override
	public List<PopularizeStatistic> statisticPopularizeByBiggerCategoryIdAndDate(Long biggerCategoryId, Date startDate, Date endDate) {
		
		startDate = DateTimeUtil.getDateDay(startDate);
		endDate = DateTimeUtil.getDateAndMaxTime(endDate);

		List<PopularizeStatistic> statistics = this.popularizeStatisticService.getPopularizeStatisticByBiggerCategoryId(biggerCategoryId);
		
		if(statistics.size() > 0){
			this.popularizeStatisticService.countVisitIPByBiggerCategoryIdAndDate(biggerCategoryId, startDate, endDate, statistics);
			List<UserOrderDayStats> userOrderDayStatses = this.userOrderDayStatsMapper
					.sumUserOrderByBiggerCatogoryId(biggerCategoryId, startDate, endDate);
			for (UserOrderDayStats stats : userOrderDayStatses) {
				for (PopularizeStatistic statistic : statistics) {
					if (statistic.getCategoryId().equals(stats.getCategoryId())) {
						statistic.setRegisterCount(stats.getRegisteredUserCount());
						statistic.setNewTradeUserCount(stats.getNewTradeUserCount());
						statistic.setOrderCount(stats.getOrderCount());
						statistic.setPaidOrderCount(stats.getSuccessOrderCount());
						statistic.setPaymentAmount(stats.getSuccessOrderAmount());
					}
				}
			}
		}
		
		return this.popularizeStatisticService.getNotStopPopularizeStatistics(biggerCategoryId, statistics);
	}

	@Override
	public List<PopularizeStatistic> statisticPopularizeByCategoryIdAndDate(Long biggerCategoryId, Long categoryId, Date startDate, Date endDate) {
		
		startDate = DateTimeUtil.getDateDay(startDate);
		endDate = DateTimeUtil.getDateAndMaxTime(endDate);
		
		List<PopularizeStatistic> statistics = this.popularizeStatisticService.getPopularizeStatisticByCategoryId(
				biggerCategoryId, categoryId);
		
		if(statistics.size() > 0){
			this.popularizeStatisticService.countVisitIPByCategoryIdAndDate(biggerCategoryId, categoryId, startDate, endDate, statistics);
			List<UserOrderDayStats> userOrderDayStatses = this.userOrderDayStatsMapper
					.sumUserOrderByCatogoryId(categoryId, startDate, endDate);
			for (UserOrderDayStats stats : userOrderDayStatses) {
				for (PopularizeStatistic statistic : statistics) {
					if (statistic.getPopularizeId().equals(stats.getPopularizeId())) {
						statistic.setRegisterCount(stats.getRegisteredUserCount());
						statistic.setNewTradeUserCount(stats.getNewTradeUserCount());
						statistic.setOrderCount(stats.getOrderCount());
						statistic.setPaidOrderCount(stats.getSuccessOrderCount());
						statistic.setPaymentAmount(stats.getSuccessOrderAmount());
					}
				}
			}
		}
		
		return this.popularizeStatisticService.getNotStopPopularizeStatistics(biggerCategoryId, categoryId, statistics);
	}

}
